package com.budwk.app.sqfwV2.service.landscapePatrol;

import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.excelService.CompleteFlowExcelService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowExcelParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwV2.dto.LandscapePatrolDTO;
import com.budwk.app.sqfwV2.enums.LandscapePatrolEnum;
import com.budwk.app.sqfwV2.generator.landscapePatrol.LandscapePatrolExcelGenerator;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;

@IocBean
public class LandscapePatrolExcelService extends CompleteFlowExcelService {


    @Override
    protected void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    public List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList) {
        List<ExcelGeneratorConfig> excelGeneratorConfigs=new ArrayList<>();
        ExcelGeneratorConfig generatorConfig=new ExcelGeneratorConfig();
        String code = excelCreateConfigList.get(0).getFileIdentity();
        generatorConfig.setExcelGenerator(new LandscapePatrolExcelGenerator(LandscapePatrolEnum.getPdfTemplat(Integer.parseInt(code))));
        generatorConfig.setExcelCreateConfig(excelCreateConfigList.get(0));
        excelGeneratorConfigs.add(generatorConfig);
        return excelGeneratorConfigs;
    }

    @Override
    public AbstractCustomerPdfCreateField getAbstractCustomerField(String jsonStr) {
        return JSONUtil.toBean(jsonStr, LandscapePatrolDTO.class);
    }
    @Override
    protected List<ExcelRowDataParam> getExcelRowData(CompleteFlowExcelParamForController paramForController) {
        String sqfwId = paramForController.getSqfwId();

        if (StringUtils.isBlank(sqfwId)) return Lists.newArrayList();
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        String code = paramForController.getExcelCreateConfigList().get(0).getFileIdentity();
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(Sqfw_formSummary_new::getSqfwId,"=",sqfwId)
  .and("param_data->>'patrolType'","=", LandscapePatrolEnum.getName(Integer.parseInt(code))).desc(Sqfw_formSummary_new::getCreateDate));
        return transformFormSummaryToExcelRowData(formSummaryNewList);
    }

    @Override
    protected List<ExcelRowDataParam> getAdminExcelRowData(CompleteFlowExcelParamForController paramForController) {
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        String code = paramForController.getExcelCreateConfigList().get(0).getFileIdentity();
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(Sqfw_formSummary_new::getId, "IN", paramForController.getFormSummaryIdList())
                .and("param_data->>'patrolType'","=", LandscapePatrolEnum.getName(Integer.parseInt(code))).desc(Sqfw_formSummary_new::getCreateDate));
        return transformFormSummaryToExcelRowData(formSummaryNewList);
    }
}
